package com.mogujie.tt.ui.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mogujie.tt.R;
import com.mogujie.tt.app.IMApplication;
import com.mogujie.tt.app.UpdateDetection;
import com.mogujie.tt.ui.base.QZBaseActivity;
import com.mogujie.tt.ui.base.TTBaseActivity;
import com.mogujie.tt.utils.FileUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;

import de.greenrobot.event.EventBus;

/**
 * Created by user on 2015/10/29.
 */
public class AboutCircleActivity extends QZBaseActivity {

    private View clearView;//清除图片缓存
    private View functionIntroductionView;//功能介绍
    private View systemInformsView;//系统通知
    private View checkUpdateView;//检查新版本
    private View gesturesView;//图案解锁
    private View setBasicInformation;//设置基本信息

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater.from(this).inflate(R.layout.tt_activity_aboutcircle, topContentView);
        showTopInformation("关于圈子");
        initView();
        controlManipulation();
    }

    private void initView(){
        clearView =(RelativeLayout)findViewById(R.id.rl_switch_notification);
        functionIntroductionView =(RelativeLayout)findViewById(R.id.rl_switch_sound);
        systemInformsView =(RelativeLayout)findViewById(R.id.rl_switch_vibrate);
        checkUpdateView =(RelativeLayout)findViewById(R.id.rl_switch_speaker);
        gesturesView = (RelativeLayout)findViewById(R.id.design_unlock);
        setBasicInformation = (RelativeLayout)findViewById(R.id.set_basic_information);


    }

    private void controlManipulation() {

        clearView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(AboutCircleActivity.this, android.R.style.Theme_Holo_Light_Dialog));
                LayoutInflater inflater = (LayoutInflater)AboutCircleActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View dialog_view = inflater.inflate(R.layout.tt_custom_dialog, null);
                final EditText editText = (EditText)dialog_view.findViewById(R.id.dialog_edit_content);
                editText.setVisibility(View.GONE);
                TextView textText = (TextView)dialog_view.findViewById(R.id.dialog_title);
                textText.setText(R.string.clear_cache_tip);
                builder.setView(dialog_view);

                builder.setPositiveButton(getString(R.string.tt_ok), new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ImageLoader.getInstance().clearMemoryCache();
                        ImageLoader.getInstance().clearDiskCache();
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                FileUtil.deleteHistoryFiles(new File(Environment.getExternalStorageDirectory().toString()
                                        + File.separator + "MGJ-IM" + File.separator), System.currentTimeMillis());
                                Toast toast = Toast.makeText(AboutCircleActivity.this,R.string.thumb_remove_finish,Toast.LENGTH_LONG);
                                toast.setGravity(Gravity.CENTER,0,0);
                                toast.show();
                            }
                        },500);

                        dialog.dismiss();
                    }
                });
                builder.setNegativeButton(getString(R.string.tt_cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
                builder.show();
            }
        });

        checkUpdateView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new UpdateDetection(AboutCircleActivity.this).checkUpdateInfo();
            }
        });

        gesturesView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(AboutCircleActivity.this, GesturesActivity.class));
            }
        });

    }

    /**
     * 显示顶部信息
     */
    private void showTopInformation(String test){
        if(test == null){
            return;
        }
        setTopBarDisplay();
        setCenterText(test);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        onDest();
    }

    public void onClickExit(View view){
        finish();
    }
}
